var xl = require('node-xlrd');
var xlsparser = require('excel-parser');


xl.open('./test.xls', function(err,bk){

    if(err) {
        console.log('报错');
        return console.log(err.name, err.message);
    }

    console.log('读取成功，等待数据传回..............');

    var shtCount = bk.sheet.count;
    console.log('excel中sheet表个数：'+ shtCount);

    for(var sIdx = 0; sIdx < shtCount; sIdx++ ){

        var sht = bk.sheets[sIdx],
            rCount = sht.row.count, //行数
            cCount = sht.column.count; //列数

        console.log('正在查看的是sheet表 "%d" ', sIdx);
        console.log('正在查看的是sheet表名称是 "%s" ', sht.name);

        //判断是否加载sheet
        console.log('  check loaded : %s', bk.sheet.loaded(sIdx) );

        console.log('  name = %s; index = %d; 行数 = %d; 列数 = %d', sht.name, sIdx, rCount, cCount);

        var student_id = [];//考试号
        var student_name = [];//姓名
        var student_china = [];//语文
        var student_math = [];//数学
        var student_english = [];//英语
        var student_physics = [];//物理
        var student_chemistry = [];//化学
        var student_biology = [];//生物
        var student_politics = [];//政治
        var student_sum = [];//总分

        for(var rIdx = 0; rIdx < rCount; rIdx++){
            for(var cIdx = 0; cIdx < cCount; cIdx++){
                switch(cIdx)
                {
                    case 0:
                        student_id[rIdx] = sht.cell(rIdx,cIdx);
                        break;
                    case 1:
                        student_name[rIdx] = sht.cell(rIdx,cIdx);
                        break;
                    case 2:
                        student_china[rIdx] = sht.cell(rIdx,cIdx);
                        break;
                    case 3:
                        student_math[rIdx] = sht.cell(rIdx,cIdx);
                        break;
                    case 4:
                        student_english[rIdx] = sht.cell(rIdx,cIdx);
                        break;
                    case 5:
                        student_physics[rIdx] = sht.cell(rIdx,cIdx);
                        break;
                    case 6:
                        student_chemistry[rIdx] = sht.cell(rIdx,cIdx);
                        break;
                    case 7:
                        student_biology[rIdx] = sht.cell(rIdx,cIdx);
                        break;
                    case 8:
                        student_politics[rIdx] = sht.cell(rIdx,cIdx);
                        break;
                    case 9:
                        student_sum[rIdx] = sht.cell(rIdx,cIdx);
                        break;
                }

                // try{
                //     console.log('  cell : row = %d, col = %d, value = "%s"', rIdx, cIdx, sht.cell(rIdx,cIdx));
                // }catch(e){
                //     console.log(e.message);
                // }
            }
        }
        console.log(student_biology);


        //save memory
        //console.log('  try unloading : index %d', sIdx );
        //if(bk.sheet.loaded(sIdx))
        //	bk.sheet.unload(sIdx);
        //console.log('  check loaded : %s', bk.sheet.loaded(sIdx) );
    }
    // if onDemand == false, allow function 'workbook.cleanUp()' to be omitted,
    // because it is called by caller 'node-xlrd.open()' after callback finished.
    //bk.cleanUp();
});